using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSRI.HelperClasses;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Xml;
using System.IO;

namespace SMS.VE.Library.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class KmlManagerFixture
    {
        public KmlManagerFixture()
        {}

        [TestMethod]
        public void KmlWithPlacemarksAndPathsSavesTheKmlFile()
        {
            KmlManager manager = new KmlManager();

            string tempFileName = System.IO.Path.GetTempFileName();

            Kml kml = new Kml();

            kml.Placemarks.Add(new Placemark("I'm at Kirkland","Tom", new LatLong(-122.2075,47.68166999999999)));
            kml.Placemarks.Add(new Placemark("I'm at Redmond","Tom", new LatLong(-122.13099,47.67861000000001)));

            Path path = new Path();
            path.Coordinates.Add(new LatLong(-122.2075, 47.68166999999999));
            path.Coordinates.Add(new LatLong(-122.13099, 47.67861000000001));
            kml.Paths.Add(path);

            using(XmlWriter writer = XmlWriter.Create(tempFileName))
            {
               manager.WriteKml(writer, kml);
            }

            Console.Out.WriteLine(File.ReadAllText(tempFileName));
            Assert.IsTrue(File.Exists(tempFileName));
        }

        [TestMethod]
        public void EmptyMessageListCreatesAnEmptyKml()
        {
            KmlManager manager = new KmlManager();

            Kml kml = manager.CreateKml(new SmsExtension[] { });

            Assert.IsNotNull(kml);
            Assert.AreEqual(0, kml.Paths.Count);
            Assert.AreEqual(0, kml.Placemarks.Count);
        }

        [TestMethod]
        public void SmsExtensionToPlacemarkMapsNameDescriptionAndPoint()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(-122.2075, 47.68166999999999), "I'm at Kirkland", "Tom")
            };

            Kml kml = manager.CreateKml(messages);

            Assert.AreEqual(0, kml.Paths.Count);
            Assert.AreEqual(1, kml.Placemarks.Count);
            Assert.AreEqual("Tom", kml.Placemarks[0].Name);
            Assert.AreEqual("I'm at Kirkland", kml.Placemarks[0].Description);
            Assert.AreEqual(new LatLong(-122.2075, 47.68166999999999), kml.Placemarks[0].Point);
        }

        [TestMethod]
        public void TwoMessagesWithDifferentLocationAndSameSenderNameGeneratesAPathBetweenLocations()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(-122.2075, 47.68166999999999), "I'm at Kirkland", "Tom"),
                new SmsExtension(new SmsClass(), new LatLong(-122.13099,47.67861000000001), "I'm at Redmond", "Tom")
            };

            Kml kml = manager.CreateKml(messages);

            Assert.AreEqual(1, kml.Paths.Count);
            Assert.AreEqual(2, kml.Paths[0].Coordinates.Count);
            Assert.AreEqual(new LatLong(-122.2075, 47.68166999999999), kml.Paths[0].Coordinates[0]);
            Assert.AreEqual(new LatLong(-122.13099, 47.67861000000001), kml.Paths[0].Coordinates[1]);
        }

        [TestMethod]
        public void TwoDifferentMessagesFromTheSameSenderNameAndLocationGeneratesOneSinglePlacemarkWithBothMessages()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(-122.2075, 47.68166999999999), "I'm at Kirkland.", "Tom"),
                new SmsExtension(new SmsClass(), new LatLong(-122.2075, 47.68166999999999), "And I'm going to Redmond.", "Tom")
            };

            Kml kml = manager.CreateKml(messages);

            Assert.AreEqual(1, kml.Placemarks.Count);
            Assert.AreEqual("Tom", kml.Placemarks[0].Name);
            Assert.AreEqual("I'm at Kirkland.And I'm going to Redmond.", kml.Placemarks[0].Description);
            Assert.AreEqual(new LatLong(-122.2075, 47.68166999999999), kml.Placemarks[0].Point);
        }

        [TestMethod]
        public void ValidLinkTemplateReturnsFormattedAnswerLink()
        {
            KmlManager manager = new KmlManager();
            manager.LinkTemplate = @"http://localhost:51424/SMS.VE.Host.Web/AnswerSMS.aspx?SenderName={0}&amp;Message={1}&amp;Number={2}";

            SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom");
            message.Sms.Number = "4444-5555";

            Assert.AreEqual(" <a href=\"http://localhost:51424/SMS.VE.Host.Web/AnswerSMS.aspx?SenderName=Tom&amp;Message=Message description&amp;Number=4444-5555\">Answer</a>", manager.GenerateAnswerLink(message));
        }

        [TestMethod]
        public void NullLinkTemplateReturnEmptyAnswerLink()
        {
            KmlManager manager = new KmlManager();
            manager.LinkTemplate = null;

            SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom");

            Assert.AreEqual(string.Empty, manager.GenerateAnswerLink(message));
        }

        [TestMethod]
        public void EmptyLinkTemplateReturnEmptyAnswerLink()
        {
            KmlManager manager = new KmlManager();
            manager.LinkTemplate = string.Empty;

            SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom");

            Assert.AreEqual(string.Empty, manager.GenerateAnswerLink(message));
        }
    }
}
